package com.ldzy.controller;

import com.github.pagehelper.Page;
import com.ldzy.pojo.*;
import com.ldzy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/selectNewbooks")
    public ModelAndView BookController(){
        Page<Student> students=bookService.selectNewBooks();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("books_new");
        modelAndView.addObject("pageResult",students.getResult());
        return modelAndView;
    }


    @RequestMapping("/selectPageByIndex")
    public ModelAndView selectPageByIndex(int index){
        Page<Student> students=bookService.selectPageByIndex(index);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("books_new");
        modelAndView.addObject("pageResult",students.getResult());
        return modelAndView;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Result<Student> findById(int id){
        Student student=bookService.findById(id);
        if(student==null){
            return new Result<Student>(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
        }else {
            return new Result<Student>(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode(),student);
        }
    }
    @RequestMapping("/deleteB")
    @ResponseBody
    public Result<Student> deleteById(int id){
        bookService.deleteById(id);
        Result result=new  Result (true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        return result;
    }
    @RequestMapping("/search")
    public ModelAndView search(){
        return BookController();
    }

    @RequestMapping("/searchBorrowed")
    public ModelAndView searchBorrowed(){
        Page<Achievement> achievements =bookService.searchBorrowed();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("books");
        modelAndView.addObject("pageResult",achievements.getResult());
        return modelAndView;
    }

}
